package by.ostrovsky.webhousingsystem.bean.wizard;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import by.ostrovsky.webhousingsystem.bean.data.EmployeBean;
import by.ostrovsky.webhousingsystem.bean.data.HousingRequestBean;
import by.ostrovsky.webhousingsystem.bean.data.TaskGroupBean;
import by.ostrovsky.webhousingsystem.bean.data.WorkPlanBean;
import by.ostrovsky.webhousingsystem.constants.Constants;
import by.ostrovsky.webhousingsystem.controller.wizard.WizardController;
import by.ostrovsky.webhousingsystem.exception.OperationErrorException;
import by.ostrovsky.webhousingsystem.hibernate.entity.Employee;
import by.ostrovsky.webhousingsystem.hibernate.entity.TaskGroup;
import by.ostrovsky.webhousingsystem.hibernate.entity.UserRequest;
import by.ostrovsky.webhousingsystem.hibernate.entity.WorkPlan;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class WizardAction extends ActionSupport implements SessionAware{
	private static Logger logger = Logger.getLogger(WizardAction.class);
	private SessionMap<String, Object> sessionMap;

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.sessionMap = (SessionMap<String, Object>) arg0;
	}

	@Override
	public String execute() {
		try {
			WizardController wizardController = new WizardController();

			EmployeBean emBean = (EmployeBean) sessionMap.get("employeBean");
			HousingRequestBean hrBean = (HousingRequestBean) sessionMap.get("requestBean");
			WorkPlanBean wpBean = (WorkPlanBean) sessionMap.get("workplanBean");
			if(wpBean == null)
				wpBean = new WorkPlanBean();
			TaskGroupBean tgBean = (TaskGroupBean) sessionMap.get("taskgroupBean");
			if(tgBean == null)
				tgBean = new TaskGroupBean();

			// starting wizard operation...

			// step 1: create tg
			// step 2: appoint employes to the tg
			// step 3: create wp record
			// step 4: mark accepted request

			// step 5: write to db
			// step 6: update view

			try {
				Map<String, Object> map = new LinkedHashMap<String, Object>();

				List<Employee> emList = new LinkedList<Employee>();
				TaskGroup tg = null;
				UserRequest hr = null;
				WorkPlan wp = null;

				tg = tgBean.initTaskGroup(hrBean, tgBean);

				if (!hrBean.getSelectedEntities().isEmpty())
					hr = hrBean.getSelectedEntities().get(0);
				else
					throw new OperationErrorException();

				wp = wpBean.initWorkplan(tg, hr);

				emBean.appointEmployes(tg);
				emList = emBean.getSelectedEntities();

				boolean check = false;
				for (Employee emp : emList) {
					if (emp.getIsMaster()) {
						check = true;
						break;
					}
				}
				if (!check)
					throw new OperationErrorException();

				map.put("tg", tg);
				map.put("em", emList);
				map.put("wp", wp);
				map.put("hr", hr);

				wizardController.save(map);

				sessionMap.remove("requestBean");
				sessionMap.remove("employeBean");
				sessionMap.remove("taskgroupBean");
				sessionMap.remove("workplanBean");
				
				return Constants.SUCCESS;

			} catch (Exception e) {
				throw new OperationErrorException();
			}
		} catch (Exception e) {
			logger.error(e);
			return Constants.FAILURE;
		}
	}
}
